import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
import numpy as np
import csv
import random
from datetime import datetime
from time import strftime

'''
to calculate the false nagetive, false positive

'''

def getCurTime():
    """
    get current time
    Return value of the date string format(%Y-%m-%d %H:%M:%S)
    """
    format='%Y-%m-%d %H:%M:%S'
    sdate = None
    cdate = datetime.now()
    try:
        sdate = cdate.strftime(format)
    except:
        raise ValueError
    return sdate

def build_data_list():
    #print getCurTime()
    sKey = np.array([])
    fn = unitCSV    # input csv location
    ra = csv.DictReader(file(fn), dialect="excel")
    
    for record in ra:
        temp = np.array([])
        for i in range(1,1003):
            temp = np.append(temp, float(record[ra.fieldnames[i]]))
        #temp = [int(float(record["ID"])), int(float(record[cancerFN])), int(float(record[popFN])), float(record[cancerFN])/float(record[popFN]), 0.0]
        sKey = np.append(sKey, temp)

    sKey.shape = (1000,-1)
    return sKey

def build_unit_list(cancerFN):
    sKey = np.array([])
    #read contiguity file
    fn = unitCSV

    #print "Strat building csv lists..."

    ra = csv.DictReader(file(fn), dialect="excel")
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        temp = [int(float(record[cancerFN])),int(float(record[popFN])),float(record["Area"]), 0.0]
        #print temp
        sKey =np.append(sKey, temp)
    sKey.shape=(-1,4)
    return sKey

def build_region_list(inputCSV):
    fn = inputCSV
    ra = csv.DictReader(file(fn), dialect="excel")
    temp_list = np.array([])
    i = 0
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        temp_list = np.append(temp_list, int(float(record[ra.fieldnames[-1]])))
        i = i + 1
    return temp_list

def build_max_p_region_list(inputCSV):
    fn = inputCSV
    #ra = csv.DictReader(file(fn), dialect="excel")
    ra = csv.reader(file(fn, 'rb'))
    temp_list = np.array([])
    i = 0
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        #temp_list = np.append(temp_list, int(float(record[ra.fieldnames[-1]])))
        temp_list = np.append(temp_list, int(float(record[-1])))
        i = i + 1
    return temp_list

def cal_region_attri(region_id):
    dis_reg = np.unique(region_id)
    iLen = dis_reg.shape[0]
    temp_reg_attri = np.zeros((iLen, 4)) #[caner, pop, area, rate]

    # unit_attri = [cancer, pop, area, rate]

    i = 0
    for item in unit_attri:
        temp_reg_attri[int(region_id[i]),0] += item[0]
        temp_reg_attri[int(region_id[i]),1] += item[1]
        #temp_reg_attri[int(region_id[i]),2] + = item[2]
        i = i + 1

    for item in temp_reg_attri:
        item[3] = item[0]/item[1]

    return temp_reg_attri[:,3]


def cal_unit_rate():           
    region_id = build_max_p_region_list(regionCSV)  
    # region_id = build_region_list(regionCSV)
    reg_rate = cal_region_attri(region_id) # [rate]
    i = 0
    for item in unit_attri:
        item[3] = reg_rate[int(region_id[i])]
        i += 1 

def cal_riskare_attri():
    temp_popcancer = np.array([])
    temp_high_popcancer = np.array([0,0,0.0]) #[cancer, pop, area]
    temp_low_popcancer = np.array([0,0,0.0]) #[cancer, pop, area]
    
    temp_cancer, temp_pop, temp_area = cal_list_pop(H1)
    temp_popcancer = np.append(temp_popcancer, [temp_cancer, temp_pop, temp_area])
    temp_high_popcancer[0] = temp_high_popcancer[0] + temp_cancer
    temp_high_popcancer[1] = temp_high_popcancer[1] + temp_pop
    temp_high_popcancer[2] = temp_high_popcancer[2] + temp_area
    
    temp_cancer, temp_pop, temp_area = cal_list_pop(L2)
    temp_popcancer = np.append(temp_popcancer, [temp_cancer, temp_pop, temp_area])
    temp_low_popcancer[0] = temp_low_popcancer[0] + temp_cancer
    temp_low_popcancer[1] = temp_low_popcancer[1] + temp_pop
    temp_low_popcancer[2] = temp_low_popcancer[2] + temp_area
    
    temp_cancer, temp_pop, temp_area = cal_list_pop(H3)
    temp_popcancer = np.append(temp_popcancer, [temp_cancer, temp_pop, temp_area])
    temp_high_popcancer[0] = temp_high_popcancer[0] + temp_cancer
    temp_high_popcancer[1] = temp_high_popcancer[1] + temp_pop
    temp_high_popcancer[2] = temp_high_popcancer[2] + temp_area
    
    temp_cancer, temp_pop, temp_area = cal_list_pop(H4)
    temp_popcancer = np.append(temp_popcancer, [temp_cancer, temp_pop, temp_area])
    temp_high_popcancer[0] = temp_high_popcancer[0] + temp_cancer
    temp_high_popcancer[1] = temp_high_popcancer[1] + temp_pop
    temp_high_popcancer[2] = temp_high_popcancer[2] + temp_area
    
    temp_cancer, temp_pop, temp_area = cal_list_pop(L5)
    temp_popcancer = np.append(temp_popcancer, [temp_cancer, temp_pop, temp_area])
    temp_low_popcancer[0] = temp_low_popcancer[0] + temp_cancer
    temp_low_popcancer[1] = temp_low_popcancer[1] + temp_pop
    temp_low_popcancer[2] = temp_low_popcancer[2] + temp_area
    
    temp_cancer, temp_pop, temp_area = cal_list_pop(L6)
    temp_popcancer = np.append(temp_popcancer, [temp_cancer, temp_pop, temp_area])
    temp_low_popcancer[0] = temp_low_popcancer[0] + temp_cancer
    temp_low_popcancer[1] = temp_low_popcancer[1] + temp_pop
    temp_low_popcancer[2] = temp_low_popcancer[2] + temp_area
    
    temp_cancer, temp_pop, temp_area = cal_list_pop(H7)
    temp_popcancer = np.append(temp_popcancer, [temp_cancer, temp_pop, temp_area])
    temp_high_popcancer[0] = temp_high_popcancer[0] + temp_cancer
    temp_high_popcancer[1] = temp_high_popcancer[1] + temp_pop
    temp_high_popcancer[2] = temp_high_popcancer[2] + temp_area
    
    temp_popcancer.shape = (-1, 3)
    return temp_popcancer, temp_high_popcancer, temp_low_popcancer

def cal_list_pop(list):
    # calculate the total pop and cancer in the input list
    temp_pop = 0
    temp_cancer = 0
    temp_area = 0
    # unit_attri = [cancer, pop, area, rate]
    for item in list:
        temp_pop = temp_pop + unit_attri[item, 1]
        temp_cancer = temp_cancer + unit_attri[item, 0]
        temp_area = temp_area + unit_attri[item, 2]
    return temp_cancer, temp_pop, temp_area

def cal_TP():
    # [threshold, count_TP, count_FN, count_FP, count_TP/(FN+FP), pop_TP, pop_FN, pop_FP, pop_TP/(FN+FP)]
    high_measure = np.array([])
    low_measure = np.array([])
    
    for t_threshold in threshold:
        temp_high_measure = np.zeros(9)
        temp_low_measure = np.zeros(9)
        temp_high_measure[0] = t_threshold[1]
        temp_low_measure[0] = t_threshold[0]
        i = 0
        for item in unit_attri:  # unit_attri = [cancer, pop, area, rate]
            if item[3] < t_threshold[0]:
                if i in low_risk_area_id:
                    temp_low_measure[1] += 1
                    temp_low_measure[5] += item[returnID]
                else:
                    temp_low_measure[3] += 1
                    temp_low_measure[7] += item[returnID] 
            if item[3] > t_threshold[1]:
                if i in high_risk_area_id:
                    temp_high_measure[1] += 1
                    temp_high_measure[5] += item[returnID]
                else:
                    temp_high_measure[3] += 1
                    temp_high_measure[7] += item[returnID] 
            i += 1
        temp_high_measure[6] = high_riskarea_attri[returnID] - temp_high_measure[5]
        temp_low_measure[6] = low_riskarea_attri[returnID] - temp_low_measure[5]
        temp_high_measure[2] = len(high_risk_area_id) - temp_high_measure[1]
        temp_low_measure[2] = len(low_risk_area_id) - temp_low_measure[1]
        
        high_measure = np.append(high_measure, temp_high_measure)
        low_measure = np.append(low_measure, temp_low_measure)

    high_measure.shape = (-1,9)
    low_measure.shape = (-1,9)
    
    for item in high_measure:
        item[4] = item[1]/(item[2]+item[3])
        item[8] = item[5]/(item[6]+item[7])


    for item in low_measure:
        item[4] = item[1]/(item[2]+item[3])
        item[8] = item[5]/(item[6]+item[7])

    temp_count_measure = np.array([repeat])
    temp_pop_measure = np.array([repeat])
    
    temp_measure = high_measure[np.argsort(high_measure[:,4]),:]
    temp_count_measure = np.append(temp_count_measure, temp_measure[-1,0:5])
    temp_measure = high_measure[np.argsort(high_measure[:,8]),:]
    temp_pop_measure = np.append(temp_pop_measure, temp_measure[-1,0])
    temp_pop_measure = np.append(temp_pop_measure, temp_measure[-1,5:])

    temp_measure = low_measure[np.argsort(low_measure[:,4]),:]
    temp_count_measure = np.append(temp_count_measure, temp_measure[-1,0:5])
    temp_measure = low_measure[np.argsort(low_measure[:,8]),:]
    temp_pop_measure = np.append(temp_pop_measure, temp_measure[-1,0])
    temp_pop_measure = np.append(temp_pop_measure, temp_measure[-1,5:])  

    #np.savetxt("c:/temp/high_measure.csv", high_measure, delimiter=',')
    #np.savetxt("c:/temp/low_measure.csv", low_measure, delimiter=',')
    #print temp_count_measure.shape, temp_pop_measure.shape
    return temp_count_measure, temp_pop_measure

def build_smoothed_list(inputCSV):
    fn = inputCSV
    ra = csv.DictReader(file(fn), dialect="excel")
    i = 0
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        #unit_attri[i,4] = int(float(record[ra.fieldnames[-1]]))
        unit_attri[i,3] = float(record["SmoothedRate"])
        i = i + 1
#--------------------------------------------------------------------------
#MAIN

if __name__ == "__main__":
    print "begin at " + getCurTime()

    H1 = [8,16,844,915,919,921,923,924]
    L2 = [5,103,106,513,517,518,520,531,534,535,536,541]
    H3 = [63,265,267,268,333,336,337,339,340,342,343,348]
    H4 = [13,174,178,198,886,887,888,889,890]
    L5 = [146,171,182,810,811,814,815,864,867]
    L6 = [20,133,692,694,695,696,698,702,705]
    H7 = [69,70,87,88,369,370,372,442,443]
    high_risk_area_id = H1 + H3 + H4 + H7
    low_risk_area_id = L2 + L5 + L6


    #threshold = np.zeros((100,2))
    a = np.arange(0.002, 0.011, 0.00005)  # low_threshold
    b = np.arange(0.01, 0.019, 0.00005)  # high_threshold
    c = np.append(a, b)
    c.shape = (2,-1)
    threshold = np.transpose(c) # [low_threshold, high_threshold]
    
    
    unitCSV = "C:/TP1000_1m.csv"
    popFN = "pop"
    unit_attri = np.zeros((1000,4))
    dataMatrix = build_data_list()  # [area, pop, cancer1, cancer2, cancer3]
    
    unit_attri[:,1] = dataMatrix[:,1]
    unit_attri[:,2] = dataMatrix[:,0]
    #filePath = "C:/_DATA/CancerData/test/Jan15/Thousand/8000/OO_CLK/"
    #filePath = "F:/OO_WARD/"
    j = 0
    while j < 1:
        if j == 0:
            soMethod = "OO"
        elif j == 1:
            soMethod = "SO"
        elif j == 2:
            soMethod = "SS"
        else:
            print "error!"
            break

        filePath = "C:/_DATA/CancerData/test/Jan15/Thousand/max_p/"
        
        # returnID used in function find_rep_region
        returnID = 1  # 0: cancer, 1: pop, 2: area
        
        while returnID < 2:
            repeat = 1
            count_measure = np.array([])
            pop_measure = np.array([])
            
            #while repeat < 1001:
            while repeat < 1001:
                #regionCSV = filePath + "cancer"+ str(repeat) +"_smoothed_pop_8000.csv"
                regionCSV = filePath + "cancer"+ str(repeat) +"_smoothed_pop_10000.csv"
                sField_Ob = "cancer" + str(repeat)
                unit_attri[:,0] = dataMatrix[:,repeat+1] # [cancer, pop, area, rate]
                riskarea_attri, high_riskarea_attri, low_riskarea_attri = cal_riskare_attri()  # [total_cancer, total_pop, total_area]
                cal_unit_rate()
                #build_smoothed_list(regionCSV)
                
                temp_count_measure, temp_pop_measure = cal_TP()
                count_measure = np.append(count_measure, temp_count_measure)
                pop_measure = np.append(pop_measure, temp_pop_measure)

                repeat = repeat + 1

            count_measure.shape = (-1, 11)
            pop_measure.shape = (-1, 11)           
            
            print pop_measure[:,1].max(), count_measure[:,1].max(), pop_measure[:,6].max(), count_measure[:,6].max(), pop_measure[:,1].min(), pop_measure[:,6].min(),count_measure[:,1].min(), count_measure[:,6].min()
            np.savetxt(filePath+"10000_count_measure.csv", count_measure, delimiter=',')
            np.savetxt(filePath+"10000_pop_measure.csv", pop_measure, delimiter=',')

            returnID = returnID + 1
        j = j + 1

    print "end at " + getCurTime()
    print "========================================================================"  

           
